import time

from PyGameAutoAndroid import *


def log(text):
    return
    # xp.console(text)
    # xp.log(text)
    # xp.toast(text)


def tap(x, y):
    xp.tap(x, y, 0.15)


def find_color_tap(color, x, y, cx=None, cy=None):
    if cx is None:
        cx = x
    if cy is None:
        cy = y
    while True:
        ret = xp.matchColor(color, x, y)
        if not ret:
            time.sleep(0.2)
            continue
        time.sleep(0.4)
        tap(cx, cy)
        break


def tap_until_color_shown(color, x, y):
    while True:
        ret = xp.matchColor(color, x, y)
        if not ret:
            time.sleep(0.2)
            tap(x, y)
            continue
        break


def tap_when_image_shown(image, x1, y1, x2, y2):
    while True:
        ret = xp.findImage(image, x1, y1, x2, y2, 0.90)
        if not ret:
            time.sleep(0.2)
            continue
        time.sleep(0.4)
        tap(ret.x, ret.y)
        break


def wait_until_image_shown(image, x1, y1, x2, y2):
    while True:
        ret = xp.findImage(image, x1, y1, x2, y2, 0.90)
        if not ret:
            time.sleep(0.1)
            continue
        time.sleep(0.1)
        break


def check_character_ready():
    while True:
        ret1 = xp.findImage("switch.png", 671, 609, 732, 641, 0.90)
        ret2 = xp.findImage("cancel_switch.png", 674, 608, 778, 641, 0.90)
        if not ret1 and not ret2:
            time.sleep(0.1)
            continue
        break


def check_minimap():
    ret = xp.findColor("#E2D57E", "1|0|#E2D57E", 1078, 137, 1080, 138)
    if ret:
        return True
    else:
        return False


def is_map_opened():
    ret = xp.matchColor("#E9E9E9", 1236, 20)
    if ret:
        return True
    else:
        return False


def open_minimap():
    while True:
        if check_minimap():
            break
        else:
            time.sleep(0.2)
    while True:
        if is_map_opened():
            break
        else:
            tap(1078, 137)
            time.sleep(0.2)


def open_party():
    while True:
        if check_minimap():
            break
        else:
            time.sleep(0.2)
    while True:
        if is_map_opened():
            break
        else:
            tap(215, 609)
            time.sleep(0.2)


def find_power_npc(x1, y1, x2, y2):
    tap_when_image_shown("power_npc.png", x1, y1, x2, y2)


def inquire_power_npc():
    tap_when_image_shown("inquire_power_npc.png", 1151, 595, 1205, 628)


def impress_npc():
    tap_when_image_shown("impress.png", 940, 386, 992, 417)


def confirm_impress_npc():
    tap_when_image_shown("confirm_impress.png", 728, 483, 809, 519)
    tap_when_image_shown("ok_impress.png", 773, 499, 825, 531)


def heal(x, y):
    while True:
        if check_minimap():
            time.sleep(0.1)
            break
        else:
            time.sleep(0.2)
    tap(x, y)
    time.sleep(0.5)


def heal_self():
    find_color_tap("#000000", 643, 203)
    heal(652, 282)
    find_color_tap("#D0C4B4", 361, 166)
    while True:
        if check_minimap() is False:
            time.sleep(0.2)
            tap(796, 506)
            continue
        break


def part_ways_npc():
    find_color_tap("#000000", 643, 203)
    open_party()
    tap_when_image_shown("allies.png", 82, 522, 202, 561)
    tap_when_image_shown("bt_npc_sword.png", 318, 212, 369, 262)
    tap_when_image_shown("part_ways.png", 764, 580, 823, 615)
    tap_when_image_shown("ok_part_ways.png", 770, 468, 829, 500)
    while True:
        if check_minimap():
            break
        else:
            time.sleep(0.2)
            tap(1236, 22)


#   战斗相关模块
def tap_character(n):
    if n == 1:
        find_color_tap("#547986", 1049, 106)
    elif n == 2:
        find_color_tap("#547986", 1049, 249)
    elif n == 3:
        find_color_tap("#547986", 1049, 392)
    elif n == 4:
        find_color_tap("#547986", 1049, 535)
    check_character_ready()


def switch_character():
    tap_when_image_shown("switch.png", 671, 609, 732, 641)
    wait_until_image_shown("cancel_switch.png", 674, 608, 778, 641)


def use_skill(n, boost=1):
    if n == 0 and boost == 1:
        tap(920, 170)
    elif n == 0 and boost == 2:
        xp.swipe(920, 170, 1020, 170, 0.3)
    elif n == 0 and boost == 3:
        xp.swipe(920, 170, 1125, 170, 0.4)
    elif n == 0 and boost == 4:
        xp.swipe(920, 170, 1210, 170, 0.6)
    elif n == 1 and boost == 1:
        tap(920, 290)
    elif n == 1 and boost == 2:
        xp.swipe(920, 290, 1020, 290, 0.3)
    elif n == 1 and boost == 3:
        xp.swipe(920, 290, 1125, 290, 0.4)
    elif n == 1 and boost == 4:
        xp.swipe(920, 290, 1210, 290, 0.6)
    elif n == 2 and boost == 1:
        tap(920, 400)
    elif n == 2 and boost == 2:
        xp.swipe(920, 400, 1020, 400, 0.3)
    elif n == 2 and boost == 3:
        xp.swipe(920, 400, 1125, 400, 0.4)
    elif n == 2 and boost == 4:
        xp.swipe(920, 400, 1210, 400, 0.6)
    elif n == 3 and boost == 1:
        tap(920, 520)
    elif n == 3 and boost == 2:
        xp.swipe(920, 520, 1020, 520, 0.3)
    elif n == 3 and boost == 3:
        xp.swipe(920, 520, 1125, 520, 0.4)
    elif n == 3 and boost == 4:
        xp.swipe(920, 520, 1210, 520, 0.6)
    time.sleep(0.6)


def initiate_battle(all_boost=False):
    if all_boost:
        find_color_tap("#FFFFFF", 1083, 643, cx=864, cy=643)
        time.sleep(0.1)
    find_color_tap("#FFFFFF", 1083, 643)
    time.sleep(5)


def is_turn_completed():
    ret = xp.matchColor("#FFFFFF", 1083, 643)
    if ret:
        return True
    else:
        return False


def wait_between_turns():
    while True:
        if is_turn_completed() is False:
            time.sleep(1)
            continue
        time.sleep(0.5)
        break


def is_battle_completed():
    ret = xp.matchColor("#000000", 556, 331)
    if ret:
        return True
    else:
        return False


def turn1_battle_sequence():
    log("开始第1回合")
    tap_character(1)
    switch_character()
    use_skill(2, 2)
    tap_character(2)
    use_skill(2)
    tap_character(3)
    use_skill(3)
    tap_character(4)
    use_skill(3)
    initiate_battle()
    wait_between_turns()


def turn2_battle_sequence():
    log("开始第2回合")
    tap_character(1)
    use_skill(3, 2)
    tap_character(2)
    use_skill(1, 4)
    tap_character(3)
    use_skill(2)
    tap_character(4)
    use_skill(3, 3)
    initiate_battle()
    wait_between_turns()


def turn3_battle_sequence():
    log("开始第3回合")
    tap_character(1)
    switch_character()
    use_skill(3)
    tap_character(2)
    switch_character()
    use_skill(3, 4)
    tap_character(3)
    use_skill(2, 4)
    tap_character(4)
    use_skill(1)
    initiate_battle()
    wait_between_turns()


def turn4_battle_sequence():
    log("开始第4回合")
    tap_character(3)
    use_skill(2, 2)
    initiate_battle()


#   move_using_minimap:
#   点开小地图的方式移动到城镇某处
#   x, y为小地图点击位置坐标
def move_using_minimap(x, y):
    open_minimap()
    time.sleep(0.1)
    tap(x, y)


def tap_until_battle_finished():
    while True:
        if not check_minimap():
            time.sleep(0.3)
            tap(1238, 20)
            continue
        break


total_count = 0
while True:
    start_time = time.time()
    # 战斗前
    move_using_minimap(751, 388)
    time.sleep(5)
    find_power_npc(506, 268, 589, 310)
    inquire_power_npc()
    impress_npc()
    confirm_impress_npc()
    tap_until_color_shown("#000000", 640, 360)

    # 战斗中
    turn1_battle_sequence()
    turn2_battle_sequence()
    turn3_battle_sequence()
    turn4_battle_sequence()
    tap_until_battle_finished()

    # 战斗后
    move_using_minimap(442, 463)    # 回旅店
    heal_self()
    move_using_minimap(552, 448)    # 出旅店
    part_ways_npc()                 # 踢出NPC

    end_time = time.time()
    total_count += 1
    xp.toast("本轮用时：" + str(int(end_time - start_time)) + "秒。共计战斗：" + str(total_count) + "场。")
